import Phaser from 'phaser'
import { TextureKeys } from '~/consts/Keys'

export default class Laser extends Phaser.GameObjects.Container {
  constructor (scene: Phaser.Scene, x: number, y: number) {
    super(scene, x, y)

    const top = scene.add.image(0, 0, TextureKeys.LaserEnd)
        .setOrigin(0.5, 0)
    this.add(top)

    const middle = scene.add.image(0, top.y + top.displayHeight, TextureKeys.LaserMiddle)
        .setOrigin(0.5, 0)
    middle.setDisplaySize(middle.width, 200)
    this.add(middle)

    const bottom = scene.add.image(0, middle.y + middle.displayHeight, TextureKeys.LaserEnd)
        .setOrigin(0.5, 0)
        .setFlipY(true) // 镜像翻转
    this.add(bottom)

    scene.physics.add.existing(this, true) // static body, don't move
    const height = top.displayHeight + middle.displayHeight + bottom.displayHeight
    const body = this.body as Phaser.Physics.Arcade.StaticBody
    body.setSize(top.displayWidth * 0.5, height * 0.9)
    body.setOffset(-top.displayWidth * 0.25, height * 0.05)
    body.position.x = this.x + body.offset.x
    body.position.y = this.y + body.offset.y
  }
}